package com.heima.publishwmnews.listener;

import com.heima.feign.wemedia.Wemedia;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.wemedia.pojo.WmNews;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

//@Component
public class PublishNewListenter {

    @Autowired
    private Wemedia wemedia;
    @RabbitListener(queues = "dl.queue")
    public void receiveMessage(Message message){
        byte[] body = message.getBody();
        String newid = new String(body);
        //远程调用微服务，根据自媒体文章id查询自媒体文章
        ResponseResult<WmNews> responseResult = wemedia.getId(Integer.valueOf(newid));
        if (responseResult.getCode()== AppHttpCodeEnum.SUCCESS.getCode()){
            WmNews wmNews = responseResult.getData();
            if (wmNews.getStatus()==WmNews.Status.SUCCESS && wmNews.getPublishTime().before(new Date())){
                wemedia.publish(wmNews);
            }
        }
    }
}
